package logic

import (
	"context"
	"fmt"
	"github.com/jinzhu/gorm"
	"teacher1/model/mysql"

	"teacher1/studentrpc/internal/svc"
	"teacher1/studentrpc/studentrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type StudentUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStudentUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StudentUpdateLogic {
	return &StudentUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *StudentUpdateLogic) StudentUpdate(in *studentrpc.StudentUpdateRequest) (*studentrpc.StudentUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var student mysql.Student
	err := mysql.DB.Debug().Where("id=?", in.Id).Find(&student).Error
	if err != nil {
		return nil, fmt.Errorf("当前学员不在该老师名下，不允许修改")
	}
	students := mysql.Student{
		Number:    in.Number,
		IdCard:    in.IdCard,
		RealName:  in.RealName,
		Mobile:    in.Mobile,
		Job:       in.Job,
		HeadImg:   in.HeadImg,
		TeaCherId: in.TeaCherId,
		Model:     gorm.Model{ID: uint(in.Id)},
	}
	mysql.DB.Debug().Model(&students).Updates(&students)
	return &studentrpc.StudentUpdateResponse{Id: int64(student.ID)}, nil
}
